/**
 * 
 */
package com.verisette.ncos.web.card.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.corp.agi.service.bean.UserBean;
import org.corp.agi.web.action.BaseAction;
import org.corp.agi.web.constant.WebConstant;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;

import com.verisette.ncos.common.ProjectConstant;

@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class PdfDispatchAction extends BaseAction {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(PdfDispatchAction.class);
	
	private InputStream inputStream;
	private String contentType;
	private String contentDisposition;
	
	private String name;
	private String isTemp;
	private String isDownload;
	private String isPrint;
	private String isPO;
	
	public String execute(){
		
    	String fileName = null;
    	try {
    		
    		String userName = ((UserBean)httpServletRequest.getSession().getAttribute(WebConstant.SESSION_USERBEAN_KEY)).getUsername();
    		// create file
    		String path = ProjectConstant.APP_ORDER_PDF_PREVIEW_FOLDER + "\\";
    		if("Y".equals(isPO)){
    			path = ProjectConstant.APP_ORDER_PO_FOLDER + "\\";
    		} else {
	    		if("Y".equals(isTemp)){
	    			path = ProjectConstant.APP_TEMP_PDF_PREVIEW_FOLDER + "\\";
	    		}
	    		if("Y".equals(isPrint)){
	    			path = ProjectConstant.APP_ORDER_PDF_FOLDER + "\\";
	        		if("Y".equals(isTemp)){
	        			path = ProjectConstant.APP_TEMP_PDF_FOLDER + "\\";
	        		}
	    		}
    		}
			File theFile = new File(path+name);
			contentType="application/pdf";
			
			if(name!=null && !name.endsWith(".pdf")){
				path += ".pdf";
			}
			
			inputStream = new FileInputStream(theFile);
			// set extension
			fileName = theFile.getName();
			
			// set File-name to PDF Files TIS-620
			fileName = URLEncoder.encode(fileName,ProjectConstant.EXPORT_ENCODE_TIS620);
			
			contentDisposition="filename="+fileName+";"; 
			if("Y".equals(isDownload)){
				contentDisposition = "attachment;" + contentDisposition;
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			try {
				String path = ProjectConstant.BASE_APP_FOLDER + "\\RESOURCES\\PDF\\";
				File theFile = new File(path+"DefaultNameCard.pdf");
				contentType="application/pdf";
			
				inputStream = new FileInputStream(theFile);
			
				// set extension
				fileName = theFile.getName();
				
				// set File-name to UTF-8
				fileName = URLEncoder.encode(fileName, "UTF-8");
				
				contentDisposition="filename="+fileName+";";
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				logger.error(e1.getMessage(), e1);
			} catch (FileNotFoundException e2) {
				// TODO Auto-generated catch block
				logger.error(e2.getMessage(), e2);
			}
		}
    	return SUCCESS;
    }

	public String getIsPO() {
		return isPO;
	}

	public void setIsPO(String isPO) {
		this.isPO = isPO;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public String getIsTemp() {
		return isTemp;
	}

	public void setIsTemp(String isTemp) {
		this.isTemp = isTemp;
	}

	public String getIsDownload() {
		return isDownload;
	}

	public void setIsDownload(String isDownload) {
		this.isDownload = isDownload;
	}

	public String getIsPrint() {
		return isPrint;
	}

	public void setIsPrint(String isPrint) {
		this.isPrint = isPrint;
	}
}
